package uniq.bitcoiner;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetManager;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText amount;
	private EditText rate;
	private EditText date;
	private Button changer;
	private boolean calcToBitcoins;
	private AssetManager assets;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		amount = (EditText) findViewById(R.id.editText2);
		rate = (EditText) findViewById(R.id.editText1);
		date = (EditText) findViewById(R.id.editText3);
		date.setText("2010-07-18");
		changer = (Button) findViewById(R.id.button1);
		calcToBitcoins = false;
		Toast.makeText(this, "onCreate", Toast.LENGTH_LONG).show();
		assets = getAssets();
	}
	
	public void onClick(View view) {
		if(view.getId() == R.id.button2) {
			if(amount.getText().length() == 0 || rate.getText().length() == 0) {
				Toast.makeText(this, "Wpisz poprawne wartości!", Toast.LENGTH_LONG).show();
				return;
			}
			float input = Float.parseFloat(amount.getText().toString());
			float ratio = Float.parseFloat(rate.getText().toString());
			if(calcToBitcoins) {
				Toast.makeText(this, input/ratio + " !", Toast.LENGTH_LONG).show();
			} else {
				Toast.makeText(this, input*ratio + " !", Toast.LENGTH_LONG).show();
			}
		} else if(view.getId() == R.id.button1) {
			if(calcToBitcoins) {
				calcToBitcoins = false;
				changer.setText(getResources().getString(R.string.btc));
			} else {
				calcToBitcoins = true;
				changer.setText(getResources().getString(R.string.zl));
			}
		} else if(view.getId() == R.id.button3) {
			Editable dateText = date.getText();
			if(dateText.length() != 10 || dateText.charAt(4) != '-' || dateText.charAt(7) != '-') {
				Toast.makeText(this, "Wpisz poprawną datę!", Toast.LENGTH_LONG).show();
				return;
			} else {
				for(int i=0; i<10; i++) {
					if(i == 4 || i == 7) continue;
					if(!Character.isDigit(dateText.charAt(i))) {
						Toast.makeText(this, "Wpisz poprawną datę!", Toast.LENGTH_LONG).show();
						return;
					}
				}
			}
			try {
				InputStream inStream = assets.open("coindesk-bpi-close.csv");
				BufferedReader br = new BufferedReader(new InputStreamReader(inStream));
				String line = br.readLine();
				while(line != null) {
					if(line.startsWith(dateText.toString())) {
						double bitRate = Double.parseDouble(line.substring(11))*3.0568;
						rate.setText(Double.toString(bitRate));
						br.close();
						return;
					} else {
						line = br.readLine();
					}
				}
				Toast.makeText(this, "Nie znaleziono kursu", Toast.LENGTH_LONG).show();
				br.close();
				return;
			} catch (Exception e) {
				Toast.makeText(this, "eror", Toast.LENGTH_LONG).show();
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
				Toast.makeText(this, e.getClass().getName(), Toast.LENGTH_LONG).show();
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void onDestroy() {
		Toast.makeText(this, "onDestroy", Toast.LENGTH_LONG).show();
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		Toast.makeText(this, "onPause", Toast.LENGTH_LONG).show();
		super.onPause();
	}

	@Override
	protected void onRestart() {
		Toast.makeText(this, "onRestart", Toast.LENGTH_LONG).show();
		super.onRestart();
	}

	@Override
	protected void onResume() {
		Toast.makeText(this, "onResume", Toast.LENGTH_LONG).show();
		super.onResume();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		Toast.makeText(this, "onSaveInstanceState", Toast.LENGTH_LONG).show();
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onStart() {
		Toast.makeText(this, "onStart", Toast.LENGTH_LONG).show();
		super.onStart();
	}

	@Override
	protected void onStop() {
		Toast.makeText(this, "onStop", Toast.LENGTH_LONG).show();
		super.onStop();
	}

	/*@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}*/

}
